package com.firefly.web.controller.api.login;

import com.firefly.web.controller.api.ApiRestController;
import com.firefly.dao.dto.login.PhoneLoginDto;
import com.firefly.dao.vo.mc.McLoginVo;
import com.firefly.service.command.mc.McUserCommand;
import com.firefly.web.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @description: 手机号登录
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/2/10
 **/
@ApiRestController("login")
public class PhoneLoginController {
    private final McUserCommand mcUserCommand;
    private final CodeUtils codeUtils;
    @Autowired
    public PhoneLoginController(McUserCommand mcUserCommand,CodeUtils codeUtils){
        this.mcUserCommand = mcUserCommand;
        this.codeUtils = codeUtils;
    }

    // 手机号验证码登录
    @PostMapping("phone")
    public McLoginVo postPhoneLogin(@Validated PhoneLoginDto request) throws Exception {
        // 验证验证码
        codeUtils.verifyCode(request.getPhone(),request.getCode());
        // 开始登录||注册
        McLoginVo result = mcUserCommand.loginOrRegister(request.getPhone(), request.getOpenId());
        // 登录成功，删除验证码
        codeUtils.removeCode(request.getCode());
        return result;
    }
}
